package festival.game;

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JPanel;

public class TurnView extends JPanel implements ComponentListener {
    private double mTextSize;
    private Game mGame;
    
    private double mCenterX;
    private double mCenterY;
    
    public TurnView(Game game) {
        super();
        mGame = game;
        addComponentListener(this);
    }
    
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        Font font = new Font(Font.SANS_SERIF, Font.BOLD, (int) mTextSize);
        g2.setFont(font);
        
        
        drawCenteredString(g2, mGame.getCurrentActionPointCount() + " AP", 
                (int) mCenterX, (int) mCenterY / 2);
    }
    
    public void drawCenteredString(Graphics2D g2, String s, int x, int y) {
        FontMetrics fm   = g2.getFontMetrics(g2.getFont());
        java.awt.geom.Rectangle2D rect = fm.getStringBounds(s, g2);
        int textHeight = (int)(rect.getHeight());
        int textWidth  = (int)(rect.getWidth());

        int textX = x - (textWidth)/2;
        int textY = y - (textHeight)/2 + fm.getAscent();
        g2.drawString(s, textX, textY);
    }
    
    public void componentHidden(ComponentEvent arg0) {
    }

    public void componentMoved(ComponentEvent arg0) {
    }

    public void componentResized(ComponentEvent arg0) {
        initSize();
    }

    public void componentShown(ComponentEvent arg0) {
        initSize();
    }
    
    private void initSize() {
        double height = getHeight();
        double width = getWidth();
        
        mTextSize = height * 0.4;
        
        mCenterX = width / 2;
        mCenterY = height / 2;
    }
}
